package com.jinbiao.annotion3;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 运行时获取注解  首先创建一个类
 * @author EX_WLJR_ZHUANGJB
 *
 * @param <T>
 */
public class Parent<T> {
	private Class<T> entity;
	public Parent() {
		init();
	}
	
	@SuppressWarnings("unchecked")
	public List<FiledMetaUtil> init(){
		
		List<FiledMetaUtil> list =  new ArrayList<FiledMetaUtil>();
		//获得超类的泛型参数的实际类型
		entity = (Class<T>)((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
		if(this.entity!=null) {
			Field[] fields = entity.getDeclaredFields();
			for (Field field : fields) {
				//获取字段中包含 注解的属性
				FiledMeta meta = field.getAnnotation(FiledMeta.class);
				if (meta!=null) {
					FiledMetaUtil u = new FiledMetaUtil(meta, field);
					list.add(u);
				}
			}
			
			   Method[] methods = entity.getMethods();
	             
	            for(Method m:methods){
	                FiledMeta meta = m.getAnnotation(FiledMeta.class);
	                if(meta!=null){
	                    FiledMetaUtil s = new FiledMetaUtil(meta,m.getName(),m.getReturnType());
	                    list.add(s);
	                }
	            }
	            
	            Collections.sort(list,new Comparator<FiledMetaUtil>() {

					public int compare(FiledMetaUtil o1, FiledMetaUtil o2) {
						return o1.getMeta().order()-o1.getMeta().order();
					}
	            	
				});
			
		}
		return list;
	}
}
